package com.primary.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.primary.controller.request.HistoryRequest;
import com.primary.entity.Bloodbanks;
import com.primary.entity.Donorinfo;
import com.primary.entity.History;
import com.primary.entity.dto.HistoryAndDonoinfoDto;
import com.primary.entity.dto.HistoryDto;
import com.primary.mappers.BloodbanksMapper;
import com.primary.mappers.DonorinfoMapper;
import com.primary.mappers.HistoryMapper;
import com.primary.service.HistoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service("HistoryService")
public class HistoryServiceImpl implements HistoryService {
    @Resource
    HistoryMapper historyMapper;

    @Resource
    BloodbanksMapper bloodbanksMapper;

    @Resource
    DonorinfoMapper donorinfoMapper;

    /**
     * 用于分页显示信息
     *
     * @param historyRequest
     */
    @Override
    public PageInfo<Map<String, Object>> queryByPage(HistoryRequest historyRequest) {
        PageHelper.startPage(historyRequest.getPageNum(), historyRequest.getPageSize());
        return new PageInfo<>(historyMapper.listByPage(historyRequest));
    }

    /**
     * 插入一条数据
     *
     * @param historyAndDonoinfoDto
     */
    @Override
    public int insertOne(HistoryAndDonoinfoDto historyAndDonoinfoDto) {
        //插入献血记录的同时插入血液库存信息
        Bloodbanks bloodbanks = new Bloodbanks();
        bloodbanks.setDonors(historyAndDonoinfoDto.getDonors());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String donoTime = sdf.format(historyAndDonoinfoDto.getDonoTime());
        bloodbanks.setBloodInfo(historyAndDonoinfoDto.getDonors() + "_" + donoTime);
        bloodbanks.setBloodType(historyAndDonoinfoDto.getBloodType());
        bloodbanks.setDonosum(historyAndDonoinfoDto.getDonoNum());
        bloodbanks.setStatus("0");
        bloodbanks.setInstoreTime(historyAndDonoinfoDto.getDonoTime());
        bloodbanks.setOptioner(historyAndDonoinfoDto.getOptioner());
        bloodbanks.setOrigin(historyAndDonoinfoDto.getOrigin());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String instoredate = sdf2.format(historyAndDonoinfoDto.getDonoTime());
        bloodbanks.setInstoredate(instoredate);
        int rows = bloodbanksMapper.insertOne(bloodbanks);
        //插入异常时直接退出
        if (rows == 0) {
            return 0;
        }
        //插入献血记录时，确认献血人信息是否存在，存在则什么都不做，不存在则插入
        if (donorinfoMapper.queryDonerForHistory(historyAndDonoinfoDto.getDonors()) == 0) {
            Donorinfo donorinfo = new Donorinfo();
            donorinfo.setDonors(historyAndDonoinfoDto.getDonors());
            donorinfo.setPhone(historyAndDonoinfoDto.getPhone());
            donorinfo.setSex(historyAndDonoinfoDto.getSex());
            donorinfo.setAge(historyAndDonoinfoDto.getAge());
            donorinfo.setBloodType(historyAndDonoinfoDto.getBloodType());
            donorinfoMapper.insertOne(donorinfo);
        }
        History history = new History();
        history.setDonors(historyAndDonoinfoDto.getDonors());
        history.setBloodType(historyAndDonoinfoDto.getBloodType());
        history.setDonoNum(historyAndDonoinfoDto.getDonoNum());
        history.setDonoTime(historyAndDonoinfoDto.getDonoTime());
        history.setOptioner(historyAndDonoinfoDto.getOptioner());
        return historyMapper.insertOne(history);
    }

    /**
     * 更新一条数据
     *
     * @param history
     * @return
     */
    @Override
    public int updateOneById(History history) {
        return historyMapper.updateOneById(history);
    }

    /**
     * 删除一条记录
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {
        return historyMapper.deleteById(id) > 0;
    }

    /**
     * 导出献血记录
     *
     * @param actualname
     */
    @Override
    public List<HistoryDto> exportInfo(String actualname) {
        return historyMapper.exportInfo(actualname);
    }
}
